package cn.ym.aircraft;

import java.awt.image.BufferedImage;

public class Hero extends FlyingObject {
    protected int life;
    protected int fire;

    Hero() {
        super(97, 139, 20, (World.WIDTH / 2 - 97 / 2), (World.HEIGHT - 139 * 2) - 80);
        this.life = 3;
        this.fire = 100;
    }

    public void step() {
    }

    private int index = 0;

    @Override
    public BufferedImage getImage() {
        if (isLive()) {
            return Images.heros[index++ % Images.heros.length];
        }
        return null;
    }

    /**
     * 发射炸弹
     *
     * @return Bullet
     */
    public Bullet[] shoot() {
        int yStep = this.y + 30;
        int xStep1 = this.x + 20;
        int xStep2 = this.x + (this.width / 2) - 5;
        int xStep3 = this.x + this.width - 30;
        if (this.fire > 100) {
            Bullet[] bullets = new Bullet[3];
            bullets[0] = new Bullet(xStep1, yStep);
            bullets[1] = new Bullet(xStep2, yStep);
            bullets[2] = new Bullet(xStep3, yStep);
            fire -= 3;
            return bullets;
        } else if (this.fire > 0) {
            Bullet[] bullets = new Bullet[2];
            bullets[0] = new Bullet(xStep1, yStep);
            bullets[1] = new Bullet(xStep3, yStep);
            fire -= 2;
            return bullets;
        } else {
            Bullet[] bullets = new Bullet[1];
            bullets[0] = new Bullet(xStep2, yStep);
            return bullets;
        }

    }

    /**
     * 控制英雄机移动
     */
    public void moveTo(int x, int y) {
        this.x = x - this.width / 2;
        this.y = y - this.height / 2;
    }

    /**
     * 英雄机加命
     */
    public void addLife(int num) {
        this.life += num;
    }

    /**
     * 英雄机加火力
     */
    public void addFire(int num) {
        this.fire += num;
    }

    /**
     * 获取英雄机命数
     */
    public int getLife() {
        return life;
    }

    /**
     * 获取英雄机火力
     */
    public int getFire() {
        return fire;
    }

    /**
     * 英雄机减命
     */
    public void subtractLife() {
        this.life -= 10;
        if (this.life < 0){
            this.life = 0;
        }
    }

    /**
     * 清空火力
     */
    public void clearFire() {
        this.fire = 0;
    }
}
